﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EARS.App_Code
{
    public class Event
    {
        private string eventName;
        private string eventVenue;
        private string description;
        private DateTime startDate;
        private DateTime endDate;
        private string startTime;
        private string endTime;
        private int limit;
        private DateTime eventDeadline;
        private string organizerUserID;
        private string organizerName;
        private int organizerContactNo;
        private string organizerEmail;
        private string exclusiveRole;
        private string exclusiveYearOfStudy;
        private string exclusiveCourse;

        public string EventName
        {
            get { return eventName; }
            set { eventName = value; }
        }

        public string EventVenue
        {
            get { return eventVenue; }
            set { eventVenue = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }

        public string StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }

        public string EndTime
        {
            get { return endTime; }
            set { endTime = value; }
        }

        public int Limit
        {
            get { return limit; }
            set { limit = value; }
        }

        public DateTime EventDeadline
        {
            get { return eventDeadline; }
            set { eventDeadline = value; }
        }

        public string OrganizerUserID
        {
            get { return organizerUserID; }
            set { organizerUserID = value; }
        }

        public string OrganizerName
        {
            get { return organizerName; }
            set { organizerName = value; }
        }

        public int OrganizerContactNo
        {
            get { return organizerContactNo; }
            set { organizerContactNo = value; }
        }

        public string OrganizerEmail
        {
            get { return organizerEmail; }
            set { organizerEmail = value; }
        }

        public string ExclusiveRole
        {
            get { return exclusiveRole; }
            set { exclusiveRole = value; }
        }

        public string ExclusiveYearOfStudy
        {
            get { return exclusiveYearOfStudy; }
            set { exclusiveYearOfStudy = value; }
        }

        public string ExclusiveCourse
        {
            get { return exclusiveCourse; }
            set { exclusiveCourse = value; }
        }

        public Event()
        {
        }

        public Event(string eventName, string eventVenue, string description, DateTime startDate, DateTime endDate, string startTime, string endTime, int limit, DateTime eventDeadline, string exclusiveRole, string exclusiveYearOfStudy, string exclusiveCourse, string userID)
        {
            this.eventName = eventName;
            this.eventVenue = eventVenue;
            this.description = description;
            this.startDate = startDate;
            this.endDate = endDate;
            this.startTime = startTime;
            this.endTime = endTime;
            this.limit = limit;
            this.eventDeadline = eventDeadline;
            Account a = new Account();
            this.organizerUserID = userID;
            this.organizerName = a.GetNameByID(userID);
            this.organizerContactNo = a.GetContactNoByID(userID);
            this.organizerEmail = a.GetEmailByID(userID);
            this.exclusiveRole = exclusiveRole;
            this.exclusiveYearOfStudy = exclusiveYearOfStudy;
            this.exclusiveCourse = exclusiveCourse;
        }

        public Event(string eventName, string eventVenue, string description, DateTime startDate, DateTime endDate, string startTime, string endTime, int limit, DateTime eventDeadline, string organiserName, int organizerContactNo, string organizerEmail, string exclusiveRole, string exclusiveYearOfStudy, string exclusiveCourse)
        {
            this.eventName = eventName;
            this.eventVenue = eventVenue;
            this.description = description;
            this.startDate = startDate;
            this.endDate = endDate;
            this.startTime = startTime;
            this.endTime = endTime;
            this.limit = limit;
            this.eventDeadline = eventDeadline;
            this.organizerName = organiserName;
            this.organizerContactNo = organizerContactNo;
            this.organizerEmail = organizerEmail;
            this.exclusiveRole = exclusiveRole;
            this.exclusiveYearOfStudy = exclusiveYearOfStudy;
            this.exclusiveCourse = exclusiveCourse;
        }

        public Event(DateTime startDate, DateTime endDate, string startTime, string endTime)
        {
            this.startDate = startDate;
            this.endDate = endDate;
            this.startTime = startTime;
            this.endTime = endTime;
        }

        public void CreateEvent(Event e)
        {
            EventDataClass edc = new EventDataClass();
            edc.CreateEvent(e);
        }

        public void UpdateEvent(Event e, string oldEventName)
        {
            EventDataClass edc = new EventDataClass();
            edc.UpdateEvent(e, oldEventName);
        }

        public void DeleteEvent(string eventName)
        {
            EventDataClass edc = new EventDataClass();
            ParticipationDataClass pdc = new ParticipationDataClass();
            edc.DeleteEvent(eventName);
            pdc.EventDeleted(eventName);
        }

        public Event GetEvent(string eventName)
        {
            EventDataClass edc = new EventDataClass();
            Event evt = edc.GetEvent(eventName);
            return evt;
        }

        public List<string> ListOfEventName()
        {
            EventDataClass edc = new EventDataClass();
            List<string> eventList = edc.ListOfEventName();
            return eventList;
        }

        public List<string> ListOfEventName(string dateCreated)
        {
            EventDataClass edc = new EventDataClass();
            List<string> eventList = edc.ListOfEventName(dateCreated);
            return eventList;
        }

        public List<string> MyOrganizedEvent(string organizerUserID)
        {
            EventDataClass edc = new EventDataClass();
            List<string> organizedEvent = edc.MyOrganizedEvent(organizerUserID);
            return organizedEvent;
        }

        public List<string> MyOrganizedEvent(string organizerUserID, string dateCreated)
        {
            EventDataClass edc = new EventDataClass();
            List<string> organizedEvent = edc.MyOrganizedEvent(organizerUserID, dateCreated);
            return organizedEvent;
        }

        public List<string> Search(string eventName)
        {
            EventDataClass edc = new EventDataClass();
            List<string> eventList = edc.Search(eventName);
            return eventList;
        }

        public List<string> Search(string eventName, string dateCreated)
        {
            EventDataClass edc = new EventDataClass();
            List<string> eventList = edc.Search(eventName, dateCreated);
            return eventList;
        }

        public string GetDateCreated(string eventName)
        {
            EventDataClass edc = new EventDataClass();
            string dateCreated = edc.GetDateCreated(eventName);
            return dateCreated;
        }

        public bool CheckLimit(string eventName)
        {
            bool check = false;
            EventDataClass edc = new EventDataClass();
            int limit = edc.CheckLimit(eventName);
            if (limit == 0)
            {
                check = true;
            }
            else if (limit != 0)
            {
                ParticipationDataClass pdc = new ParticipationDataClass();
                int num = pdc.NumberOfParticipant(eventName);
                if (num < limit)
                {
                    check = true;
                }
            }
            return check;
        }

        public bool checkTiming(string venue, DateTime startDate, DateTime endDate, string startTime, string endTime)
        {
            bool check = true;
            EventDataClass edc = new EventDataClass();
            List<string> eventList = edc.ListOfEventToCheck(venue);
            foreach (string Event in eventList)
            {
                Event e = edc.GetTimeSlotInfo(Event);
                if (StartCheck1(startDate, startTime, e.startDate, e.startTime) == true && EndCheck1(endDate, endTime, e.endDate, e.endTime) == true)
                {
                    check = false;
                }
                else if (StartCheck2(startDate, startTime, e.startDate, e.startTime) == true || EndCheck2(endDate, endTime, e.endDate, e.endTime) == true)
                {
                    check = false;
                }
                else if (StartCheck3(startDate, startTime, e.startDate, e.startTime) == true || EndCheck3(endDate, endTime, e.endDate, e.endTime) == true)
                {
                    check = false;
                }
            }
            return check;
        }

        public bool StartCheck1(DateTime sd, string st, DateTime dd, string dt)
        {
            bool check = false;
            if (sd > dd || CustomStringToIntParse(st) > CustomStringToIntParse(dt))
            {
                check = true;
            }
            return check;
        }

        public bool EndCheck1(DateTime ed, string et, DateTime dd, string dt)
        {
            bool check = false;
            if (ed < dd || CustomStringToIntParse(et) < CustomStringToIntParse(dt))
            {
                check = true;
            }
            return check;
        }

        public bool StartCheck2(DateTime sd, string st, DateTime dd, string dt)
        {
            bool check = false;
            if (dd > sd || CustomStringToIntParse(dt) > CustomStringToIntParse(st))
            {
                check = true;
            }
            return check;
        }

        public bool EndCheck2(DateTime ed, string et, DateTime dd, string dt)
        {
            bool check = false;
            if (dd < ed || CustomStringToIntParse(dt) < CustomStringToIntParse(et))
            {
                check = true;
            }
            return check;
        }

        public bool StartCheck3(DateTime sd, string st, DateTime dd, string dt)
        {
            bool check = false;
            if (dd == sd && CustomStringToIntParse(dt) == CustomStringToIntParse(st))
            {
                check = true;
            }
            return check;
        }

        public bool EndCheck3(DateTime ed, string et, DateTime dd, string dt)
        {
            bool check = false;
            if (dd == ed && CustomStringToIntParse(dt) == CustomStringToIntParse(et))
            {
                check = true;
            }
            return check;
        }

        public int CustomStringToIntParse(string time)
        {
            int convert = 0;
            switch (time)
            {
                case "8:00 AM":
                    convert = 8;
                    break;
                case "9:00 AM":
                    convert = 9;
                    break;
                case "10:00 AM":
                    convert = 10;
                    break;
                case "11:00 AM":
                    convert = 11;
                    break;
                case "12:00 PM":
                    convert = 12;
                    break;
                case "1:00 PM":
                    convert = 13;
                    break;
                case "2:00 PM":
                    convert = 14;
                    break;
                case "3:00 PM":
                    convert = 15;
                    break;
                case "4:00 PM":
                    convert = 16;
                    break;
                case "5:00 PM":
                    convert = 17;
                    break;
                case "6:00 PM":
                    convert = 18;
                    break;
                case "7:00 PM":
                    convert = 19;
                    break;
                case "8:00 PM":
                    convert = 20;
                    break;
                case "9:00 PM":
                    convert = 21;
                    break;
            }

            return convert;
        }
    }
}